import os
from time import strftime
from XTestRunner import HTMLTestRunner
from utils.record_log import LogConfig


class CreateReportHTML:
    """
    生成html形式的测试报告类
    """
    # 生成报告的位置 - test_report目录下且按当前时间格式命名
    report_path = ('\\'.join(os.path.dirname(__file__).split('\\')[:-2]) + "\\reports\\" +
                   strftime("%Y-%m-%d_%H时%M分%S秒") + "_report.html")

    # 获取配置好的日志对象
    logger = LogConfig.get_config_logger()

    @classmethod
    def create_report_html(cls, suite):
        """
        运行测试用例 生成html报告
        :param suite: 测试用例套件
        :return:测试报告路径
        """
        try:
            with open(cls.report_path, "wb") as file_html:
                runner = HTMLTestRunner(
                    stream=file_html,
                    tester="高少",
                    title='在线商城Web自动化测试用例报告',
                    description='运行环境：windows 10',
                    language='zh-CN',
                    rerun=1
                )
                runner.run(suite)
                return cls.report_path
        except Exception as e:
            cls.logger.error("写入测试用例报告失败 : ", e)


if __name__ == "__main__":
    print()
